<?php
namespace app\admin\controller;

use app\admin\model\Article as ArticleModel;
use think\Db;
use think\Loader;

class Article extends Base
{
    // 文章列表
    public function index()
    {
        $list = ArticleModel::order('id desc')->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }

    // 添加文章
    public function add()
    {
        if (request()->isPost()) {
            $data = [
                'title'    => input('title'),
                'author'   => input('author'),
                'desc'     => input('desc'),
                'keywords' => str_replace('，', ',', input('keywords')),
                'content'  => input('content'),
                'cateid'   => input('cateid'),
                'addtime'  => time(),
            ];
            if (input('state') == 'on') {
                $data['state'] = 1;
            }

            if ($_FILES['pic']['tmp_name']) {
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                $data['pic'] = '/uploads/' . $info->getSaveName();
            }

            // 验证信息
            $validate = Loader::validate('Article');
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
                die;
            }
            $result = Db::name('article')->insert($data);
            if ($result !== false) {
                return $this->success('添加文章成功！', 'index');
            } else {
                return $this->error('添加文章失败！');
            }
            return;
        }
        $cateres = Db::name('cate')->select();
        $this->assign('cateres', $cateres);
        return $this->fetch();
    }

    // 编辑文章
    public function edit()
    {
        $id   = input('id');
        $info = Db::name('article')->find($id);

        if (request()->isPost()) {
            $data = [
                'id'       => input('id'),
                'title'    => input('title'),
                'author'   => input('author'),
                'desc'     => input('desc'),
                'keywords' => str_replace('，', ',', input('keywords')),
                'content'  => input('content'),
                'cateid'   => input('cateid'),
            ];

            if (input('state') == 'on') {
                $data['state'] = 1;
            } else {
                $data['state'] = 0;
            }

            if ($_FILES['pic']['tmp_name']) {

                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                $data['pic'] = '/uploads/' . $info->getSaveName();
            }

            // 验证信息
            $validate = Loader::validate('article');
            if (!$validate->scene('eidt')->check($data)) {
                $this->error($validate->getError());
                die;
            }
            $result = Db::name('article')->update($data);
            if ($result !== false) {
                $this->success('修改文章成功！', 'index');
            } else {
                $this->error('修改文章失败！');
            }
            return;
        }

        $this->assign('article', $info);

        $cateres = Db::name('cate')->select();
        $this->assign('cateres', $cateres);

        return $this->fetch();
    }

    // 删除 
    public function del()
    {
        $id     = input('id');
        $result = Db::name('article')->delete(input('id'));

        if ($result !== false) {
            $this->success('删除文章成功！', 'index');
        } else {
            $this->error('删除文章失败！');
        }
    }
}
